

// deviceready  DOMContentLoaded
var __pcTest = false;
var __type1 = __pcTest ? "DOMContentLoaded" : "deviceready";
var __isIOS = (/iphone|ipad/gi).test(navigator.appVersion);
var __SNShare;
var __TXShare;
var page = {};

page.init = function(){
    page.click = __pcTest ? "click" : "touchend";

    if (window.screen.height == 568) { // iPhone 5
        document.querySelector("meta[name=viewport]").content = "width=320.1,initial-scale=1,maximum-scale=1,user-scalable=0";
    }
    
    $("body").removeClass("noCss3").css({"width": $.bodyWidth() + "px", "height": $.bodyHeight() + "px"});

    var currentInput;
    $("body").delegate("input, textarea", "focus", function(e){
        currentInput = $(this);
    }).on(page.click, function(e){
        if (e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA") {
            if (currentInput) {
                currentInput.blur();
                currentInput = null;
            }
        }
    });

    document.addEventListener("resume", page.onStageResume, false);
    checkConnection();

    $("a.input").hover().on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        page.sound("audio/click.mp3");
        buttonClick($(this).attr("item"));
        return false;
    });
    $("#beforeGame #rulesBtn").hover().on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        page.sound("audio/click.mp3");
        return false;
    });
    $("#gameing .start .btn").hover();
    $("#firstBanner a.close").hover();
    $("#gameing .chooseCard .cards").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        buttonClick($(this).attr("item"));
        return false;
    });
    
    page.service = "http://demo.nadoo.cn/kia/Interface/";


    $.ajax({
        type: "GET",
        url: page.service + "GetVersion",
        dataType: "jsonp",
        success: function(e){
            if (e.version != "1.1") {
                var _title = __isIOS ? "App store有新版本更新，是否前往下载？" : "Google Play有新版本更新，是否前往下载？";
                systemConfirm(_title, function(e){
                    if (e == 1) {
                        if (__isIOS) {
                            window.location.href = e.ioshref;
                        } else {
                            navigator.app.loadUrl(e.androidhref, {openExternal: true});
                        }
                    } else {
                        return false;
                    }
                }, "更新提示", "立即下载,取消");
            }
        },
        error: function(xhr, type){
            $.ui.loading("hide");
        }
    });

    // 禁止默认的滚动条
    document.ontouchmove = stopDefault;
    function stopDefault(e) {
        if (e && e.preventDefault) {
            e.preventDefault();
        }
    }
    page.getUserCount();

    page.bgAudio("audio/bg.mp3", 0.8);

    page.firendNumScroll = new iScroll("firendNumScroll");
    page.scoreList1Scroll = new iScroll("scoreList1Scroll");
    page.scoreList2Scroll = new iScroll("scoreList2Scroll");
    $("#carScroll .c").css({"width": $.bodyWidth() * 6 + "px"});
    page.carScroll = new iScroll("carScroll", { hScrollbar: false, vScrollbar: false, snap: true, momentum: false, onScrollEnd: function (){
        $("#choose .color li.c").removeClass("c");
        $("#choose .color li.c" + (this.currPageX + 1)).addClass("c");
    }});

    $("#firstBanner .close").on(page.click, page.closeFirstBanner);
    $("#firstBanner .light").on(page.click, function(){
        $(this).toggleClass("on");
    });
    $("#firstBanner .inner").css({"width": $.bodyHeight() * 1.775 + "px", "marginLeft": -($.bodyHeight() * 1.775) / 2 + "px"}).show();

    $("#beforeGame #menu .enter").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        var _id = localStorage.getItem("id") || "0";
        if (_id != "0") {
            $.ui.loading("show", "读取中...");
            page.getUserCount(function(e){
                $.ui.loading("hide");
                if (e != -1) {
                    page.enterChoose($("#beforeGame #menu"));
                } else {
                    systemNotification("您的游戏次数已用尽，请稍等片刻再试");
                }
            });
        } else {
            page.enterLogin();
        }
        return false;
    });
    $("#beforeGame #rulesBtn").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#beforeGame #rulesBtn.show").removeClass("show");
        $("#beforeGame #choose").pageTo($("#beforeGame #rules"), {type: "scale"});
        return false;
    });
    $("#beforeGame #rules .back").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#beforeGame #rulesBtn").addClass("show");
        $("#beforeGame #rules").pageTo($("#beforeGame #choose"), {type: "scale"});
        return false;
    });
    $("#beforeGame #menu .brand").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#beforeGame #menu").pageTo($("#beforeGame #brand"), {type: "scale", callback: function(){

        }});
        return false;
    });
    $("#beforeGame #brand .back").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#beforeGame #brand").pageTo($("#beforeGame #menu"), {type: "scale", callback: function(){

        }});
        return false;
    });
    $("#beforeGame #menu .event").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#beforeGame").pageTo($("#event"), {type: "scale", callback: function(){
            $.ui.loading("show", "加载中...");
            $.ajax({
                type: "GET",
                url: page.service + "GetNotice",
                dataType: "jsonp",
                success: function(e){
                    $.ui.loading("hide");
                    if (e.status == -1) {
                        systemNotification(e.info);
                    } else if (e.status == 1) {
                        $("#event").html(e.notice);
                    }
                },
                error: function(xhr, type){
                    $.ui.loading("hide");
                }
            });
        }});
        return false;
    });
    $("#event").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#event").pageTo($("#beforeGame"), {type: "scale", callback: function(){

        }});
        return false;
    });
    $("#beforeGame #login .btn").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        var _name = $("#login .name").val();
        var _tel = $("#login .tel").val();
        var _city = $("#login .city").val();
        var _code = $("#login .code input").val();
        var _telReg = /^1[3|4|5|8][0-9]\d{8}$/;
        if (_name == "") {
            systemNotification("请输入用户名");
        } else if (_name.length > 20) {
            systemNotification("用户名太长");
        } else if (_tel == "") {
            systemNotification("请输入手机号码");
        } else if (!(_telReg.test(_tel))) {
            systemNotification("请输入有效的手机号码");
        } else if (_city == "") {
            systemNotification("请输入有效的所在城市");
        } else if (_code == "") {
            systemNotification("请输入验证码");
        } else {
            var data = {"nickname": _name, "mobile": _tel, "city": _city, "code": _code};
            console.log(data);
            $.ui.loading("show", "提交中...");
            $.ajax({
                type: "GET",
                url: page.service + "Login",
                data: data,
                dataType: "jsonp",
                success: function(e){
                    $.ui.loading("hide");
                    if (e.status == -1) {
                        systemNotification(e.info);
                    } else if (e.status == 1) {
                        page.userId = e.id;
                        localStorage.setItem("id", e.id);
                        systemNotification("注册成功", function(){
                            $.ui.loading("show", "读取中...");
                            page.getUserCount(function(e){
                                $.ui.loading("hide");
                                page.enterChoose($("#beforeGame #login"));
                            });
                        });
                    }
                },
                error: function(xhr, type){
                    $.ui.loading("hide");
                }
            });
        }
        return false;
    });
    $("#beforeGame #choose .inner .color li").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#beforeGame #choose .inner .color .c").removeClass("c");
        $(this).addClass("c");
        page.carScroll.scrollToPage($(this).index(), 0, 300);
        return false;
    });
    $("#beforeGame #choose .inner .scrolls a").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        var _x;
        if ($(this).hasClass("left")) {
            var _x = page.carScroll.currPageX - 1;
            // $("#beforeGame #choose .inner .scrolls a.right").removeClass("un");
            // if (_x <= 0) {
            //     $("#beforeGame #choose .inner .scrolls a.left").addClass("un");
            // }
        } else {
            var _x = page.carScroll.currPageX + 1;
            // $("#beforeGame #choose .inner .scrolls a.left").removeClass("un");
            // if (_x >= 5) {
            //     $("#beforeGame #choose .inner .scrolls a.right").addClass("un");
            // }
        }
        if (_x != null) {
            page.carScroll.scrollToPage(_x, 0, 300);
            
        }
        return false;
    });
    $("#beforeGame #choose .start, #beforeGame #rules .start").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $.ui.loading("show", "读取中...");
        page.getUserCount(function(e){
            $.ui.loading("hide");
            if (e != -1) {
                $("#gameing>.title").removeClass("show");
                $("#gameing .view").hide();
                $("#gameing .start").show();
                $("#gameing .blurs").removeClass("blur");
                car.carImg = "images/games/" + $("#beforeGame #choose .inner .color .c").attr("url") + ".png";
                car.onReplayClick(null);
                $("#beforeGame").pageTo($("#gameing"), {type: "scale", callback: function(){
                    $("#beforeGame #choose .inner").removeClass("animate");
                }});
            } else {
                systemNotification("您的游戏次数已用尽，请稍等片刻再试");
            }
        });
        return false;
    });
    $("#gameing .score .btns .next").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        var _count = $("#gameing>.title h6").html();
        if (_count != 0) {
            $("#beforeGame .view").hide();
            $("#beforeGame #menu").show();
            $("#beforeGame #menu .inner").removeClass("animate");
            $("#beforeGame #rulesBtn.show").removeClass("show");
            $("#gameing").pageTo($("#beforeGame"), {type: "scale", callback: function(){
                $("#beforeGame #menu .inner").addClass("animate");
            }});
        } else {
            systemConfirm("您的游戏次数已耗尽，需稍等片刻后获得游戏机会，邀请好友立即获得更多游戏机会！", function(e){
                if (e == 1) {
                    $("#shareNumber .view").hide();
                    $("#shareNumber .rules1").show();
                    $("#gameing").pageTo($("#shareNumber"), {type: "scale"});
                } else {
                    $("#beforeGame .view").hide();
                    $("#beforeGame #menu").show();
                    $("#beforeGame #menu .inner").removeClass("animate");
                    $("#beforeGame #rulesBtn.show").removeClass("show");
                    $("#gameing").pageTo($("#beforeGame"), {type: "scale", callback: function(){
                        $("#beforeGame #menu .inner").addClass("animate");
                    }});
                }
            }, "提示", "邀请好友,取消");
        }
        return false;
    });
    $("#gameing .chooseCard .btns .back").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#gameing .chooseCard").pageTo($("#gameing .score"), {type: "scale", callback: function(){
            car.updateScore();
        }});
        return false;
    });
    $("#gameing .chooseCard .btns .choose, #gameing .chooseCard .cards").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        var _id = localStorage.getItem("id") || "0";
        if (_id != "0") {
            $.ui.loading("show", "抽取中...");
            $.ajax({
                type: "GET",
                url: page.service + "Prize",
                data: {"id": _id},
                dataType: "jsonp",
                success: function(e){
                    $.ui.loading("hide");
                    if (e.status == -1) {
                        systemNotification(e.info);
                    } else if (e.status == 1) {
                        if (e.type == 0) {
                            $("#gameing .chooseCard").pageTo($("#gameing .noChouZhong"), {type: "scale"});
                        } else if (e.type == 1) {
                            $("#gameing .chouZhong .inner").removeClass("show");
                            $("#gameing .chooseCard").pageTo($("#gameing .chouZhong"), {type: "scale", callback: function(){
                                $("#gameing .chouZhong .inner").addClass("show");
                            }});
                        }
                    }
                },
                error: function(xhr, type){
                    $.ui.loading("hide");
                }
            });
        }
        return false;
    });
    $("#gameing .chouZhong .btns .shareSN").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        if (__SNShare) {
            window.open(__SNShare);
        }
        return false;
    });
    $("#gameing .chouZhong .btns .shareTX").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        if (__TXShare) {
            window.open(__TXShare);
        }
        return false;
    });
    $("#gameing .noChouZhong .btns .enter, #gameing .chouZhong .btns .enter").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#firstBanner").hide();
        $("#beforeGame .view").hide();
        $("#beforeGame #choose").show();
        var _target = $(this).parents(".chouZhong").length;
        if (_target == 0) {
            _target = $("#gameing .noChouZhong");
        } else {
            _target = $("#gameing .chouZhong");
        }
        _target.pageTo($("#gameing .score"), {type: "scale", callback: function(){
            car.updateScore();
        }});
        return false;
    });
    $("#beforeGame #login .code a").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        var _tel = $("#login .tel").val();
        var _telReg = /^1[3|4|5|8][0-9]\d{8}$/;
        if (_tel == "") {
            systemNotification("请输入手机号码");
        } else if (!(_telReg.test(_tel))) {
            systemNotification("请输入有效的手机号码");
        } else {
            $.ui.loading("show", "发送中...");
            $.ajax({
                type: "GET",
                url: page.service + "GetCode",
                data: {"mobile": _tel},
                dataType: "jsonp",
                success: function(e){
                    $.ui.loading("hide");
                    if (e.status == -1) {
                        systemNotification(e.info);
                    } else if (e.status == 1) {
                        systemNotification("验证码已经发送到您的手机号上，请注意查收");
                        clearTimeout(page.codeTimeout);
                        page.dateCode = new Date();
                        page.dateCode = Math.ceil(Date.parse(page.dateCode) / 1000);
                        runCodeTimeout();
                    }
                },
                error: function(xhr, type){
                    $.ui.loading("hide");
                }
            });
        }
        return false;
    });
    $("#gameing .score .btns .share").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#shareNumber .view").hide();
        $("#shareNumber .rules1").show();
        $("#gameing").pageTo($("#shareNumber"), {type: "scale"});
        return false;
    });
    $("#gameing .score .btns .yaoqing").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#shareNumber .view").hide();
        $("#shareNumber .rules2").show();
        $("#gameing").pageTo($("#shareNumber"), {type: "scale"});
        return false;
    });
    $("#shareNumber .rules1 .back, #shareNumber .rules2 .back").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#gameing>.title").addClass("show");
        $("#firstBanner").hide();
        $("#gameing .view").hide();
        $("#gameing .score").show();
        $("#gameing .blurs").addClass("blur");
        $("#shareNumber").pageTo($("#gameing"), {type: "scale", callback: function(){
            page.scoreList1Scroll.refresh();
            page.scoreList2Scroll.refresh();
        }});
        return false;
    });
    $("#shareNumber .rules1 .share, #shareNumber .rules2 .yaoqing").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        var _target = $("#shareNumber .rules1");
        if ($(this).hasClass("share")) {
            _target = $("#shareNumber .rules1");
            $("#shareNumber .shareP").removeClass("shareP2").addClass("shareP1");
        } else {
            _target = $("#shareNumber .rules2");
            $("#shareNumber .shareP").removeClass("shareP1").addClass("shareP2");
        }
        $("#shareNumber .shareP .list ul").empty();
        _target.pageTo($("#shareNumber .shareP"), {type: "scale", callback: function(){
            var _html = "";
            var options = new ContactFindOptions();
            options.filter = ""; // empty search string returns all contacts
            options.multiple = true; // return multiple results
            filter = ["displayName", "phoneNumbers"];
            navigator.contacts.find(filter, onSuccess, onError, options);
            function onSuccess(contacts) {
                for(var i = 0; i < contacts.length; i++){
                    if(contacts[i].phoneNumbers && contacts[i].phoneNumbers.length!=0){
                        _html += "<li num='" + contacts[i].phoneNumbers[0]["value"]+ "'>" + contacts[i].displayName + "</li>";
                    }
                }
                $("#shareNumber .shareP .list ul").html(_html);
                $("#shareNumber .shareP .list ul li").on(page.click, function(){
                    if ($.pageAnimating() || $.touchMoved) { return false;}
                    $(this).toggleClass("on");
                    return false;
                });
                page.firendNumScroll.refresh();
            }
            function onError() {}
        }});
        return false;
    });
    $("#shareNumber .shareP .back").on(page.click, function() {
        if ($.pageAnimating() || $.touchMoved) { return false;}
        if ($("#shareNumber .shareP").hasClass("shareP1")) {
            $("#shareNumber .shareP").pageTo($("#shareNumber .rules1"), {type: "scale"});
        } else {
            $("#shareNumber .shareP").pageTo($("#shareNumber .rules2"), {type: "scale"});
        }
        return false;
    });
    $("#brand .cars").swipeLeft(function(){
        if ($(this).hasClass("c1")) {
            $(this).removeClass("c1").addClass("c2");
        } else if ($(this).hasClass("c2")) {
            $(this).removeClass("c2").addClass("c3");
        }
    });
    $("#brand .cars").swipeRight(function(){
        if ($(this).hasClass("c3")) {
            $(this).removeClass("c3").addClass("c2");
        } else if ($(this).hasClass("c2")) {
            $(this).removeClass("c2").addClass("c1");
        }
    });
    $("#brand .bnr").swipeLeft(function(){
        $("#brand .bnr ul").attr("class", "show2");
    });
    $("#brand .bnr").swipeRight(function(){
        $("#brand .bnr ul").attr("class", "show1");
    });
    $("#brand .bnr .scroll.l").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#brand .bnr ul").attr("class", "show1");
        return false;
    });
    $("#brand .bnr .scroll.r").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        $("#brand .bnr ul").attr("class", "show2");
        return false;
    });
    $("#shareNumber .shareP .yaoqing, #shareNumber .shareP .share").on(page.click, function(){
        if ($.pageAnimating() || $.touchMoved) { return false;}
        if ($("#shareNumber .shareP .list ul li.on").length != 0) {
            var _nums = "", _type;
            $("#shareNumber .shareP .list ul li.on").each(function(index, num){
                var _num = $(num).attr("num");
                var _name = $(num).html();
                _num = _num.replace(/[?|&|=|amp;|\|\|\|]/gi, "");
                _name = _name.replace(/[?|&|=|amp;|\|\|\|]/gi, "");
                _nums += _name + "|||" + _num + ",";
            });
            _nums = _nums.substr(0, _nums.length - 1);
            if ($(this).hasClass("share")) {
                _type = "1";
            } else {
                _type = "2";
            }
            var _id = localStorage.getItem("id") || "0";
            if (_id != "0") {
                if (_nums == "") {
                    systemNotification("请选择好友");
                    return false;
                }
                $.ui.loading("show", "发送中...");
                $.ajax({
                    type: "GET",
                    url: page.service + "InviteMsg",
                    data: {"invites": _nums, "type": _type, "id": _id},
                    dataType: "jsonp",
                    success: function(e){
                        $.ui.loading("hide");
                        if (e.status == -1) {
                            systemNotification(e.info);
                        } else if (e.status == 1) {
                            systemNotification(e.info, function(){
                                $("#gameing>.title").addClass("show");
                                $("#firstBanner").hide();
                                $("#gameing .view").hide();
                                $("#gameing .score").show();
                                $("#gameing .blurs").addClass("blur");
                                $("#shareNumber").pageTo($("#gameing"), {type: "scale", callback: function(){
                                    page.scoreList1Scroll.refresh();
                                    page.scoreList2Scroll.refresh();
                                }});
                            });
                        }
                        page.getUserCount();
                    },
                    error: function(xhr, type){
                        $.ui.loading("hide");
                    }
                });
            }
        }
        return false;
    });

}

page.onStageResume = function() {
    var _online = checkConnection();
    if (_online) {
        page.getUserCount();
    }
}

// 获取用户游戏次数
page.getUserCount = function(callback){
    var _id = localStorage.getItem("id") || "0";
    if (_id != "0") {
        $.ajax({
            type: "GET",
            url: page.service + "GetUserCount",
            data: {"id": _id},
            dataType: "jsonp",
            success: function(e){
                console.log(e);
                $("#beforeGame #title h6, #gameing>.title h6, #gameing>.counts").show().html(e.count);
                if (callback) {
                    if (e.count >= 1) {
                        callback(e.count);
                    } else {
                        callback(-1);
                    }
                }
                if (e.nexttime) {
                    if (page.getCountTimeout) {
                        clearTimeout(page.getCountTimeout);
                    }
                    page.getCountTimeout = setTimeout(function(){
                        page.getUserCount();
                    }, (e.nexttime + 10) * 1000);
                }
            },
            error: function(xhr, type){
                $.ui.loading("hide");
            }
        });
    } else {
        $("#beforeGame #title h6, #gameing>.title h6, #gameing>.counts").hide();
    }
}

// 首页广告关闭并进入主菜单
page.closeFirstBanner = function(){
    if ($.pageAnimating() || $.touchMoved) { return false;}
    $("#beforeGame view").hide();
    $("#beforeGame #menu").show().find(".inner").removeClass("animate");
    $("#firstBanner").pageTo($("#beforeGame"), {type: "fade", callback: function(){
        $("#beforeGame #menu .inner").addClass("animate");
    }});
    return false;
}

// 主菜单进入登录
page.enterLogin = function(){
    if ($.pageAnimating() || $.touchMoved) { return false;}
    $("#beforeGame #menu .inner").removeClass("animate");
    $("#beforeGame #login .inner").removeClass("animate");
    $("#beforeGame #menu").pageTo($("#beforeGame #login"), {type: "scale", callback: function(){
        $("#beforeGame #login .inner").addClass("animate");
    }});
    return false;
}

// 进入选车
page.enterChoose = function(which){
    $("#beforeGame #rulesBtn").addClass("show");
    which.find(".inner").removeClass("animate");
    which.pageTo($("#beforeGame #choose"), {type: "scale", callback: function(){
        page.carScroll.refresh();
        $("#beforeGame #choose .inner").addClass("animate");
        $("#beforeGame #choose .inner .color .c").removeClass("c");
        $("#beforeGame #choose .inner .color .c1").addClass("c");
    }});
}

// 加载背景音乐
page.bgAudio = function(src, vol) {
    if (!__pcTest) {
        if (page.audio) {
            page.audio.stop();
        }
        if (!__isIOS) {
            src = "/android_assets/www/" + src;
        }
        page.audio = new Media(src, function(){}, function(){});
        page.audio.play({numberOfLoops: -1});
        if (vol) {
            page.audio.setVolume(vol);
        }
    } else {
        if (page.audio) {
            page.audio.pause();
        }
        if (src != null) {
            page.audio = new Audio();
            page.audio.src = src;
            page.audio.onload = function(){
                if (vol) {
                    page.audio.volume = vol;
                }
                page.audio.autoplay = "1";
                page.audio.loop = "loop";
            }
        }
    }
}

page.sound = function(src, vol) {
    if (!__pcTest) {
        if (page.audio2) {
            page.audio2.stop();
        }
        if (!__isIOS) {
            src = "/android_assets/www/" + src;
        }
        page.audio2 = new Media(src, function(){}, function(){});
        page.audio2.play({numberOfLoops: 1});
        if (vol) {
            page.audio2.setVolume(vol);
        }
    } else {
        if (page.audio2) {
            page.audio2.pause();
        }
        page.audio2 = new Audio();
        page.audio2.src = src;
        page.audio2.onload = function(){
            if (vol) {
                page.audio2.volume = vol;
            }
            page.audio2.autoplay = "1";
        }
    }
}

document.addEventListener(__type1, function() {
    $(function(){
        page.init();
    });
});

function systemNotification(message, callback, title) {
    if (!__pcTest) {
        navigator.notification.alert(
            message,//message
            callback,
            "提示",//标题
            "确定"//按钮名称
        );
    } else {
        alert(message);
        if (callback) {
            callback();
        }
    }
};

//调用系统的对话框
function systemConfirm(message, callback, title, btnName) {
    navigator.notification.confirm(
        message,
        callback,
        title,
        btnName
    );
};

function runCodeTimeout() {
    var _t = new Date();
    _t = Math.ceil(Date.parse(_t) / 1000);
    _t = 60 - (_t - page.dateCode);
    if (_t > 0) {
        $("#beforeGame #login .code").addClass("sedding").find(".timeout").html(_t + "秒");
        page.codeTimeout = setTimeout(runCodeTimeout, 500);
    } else {
        $("#beforeGame #login .code").removeClass("sedding");
        clearTimeout(page.codeTimeout);
    }
}

function checkConnection() {
    if (__pcTest) {
        return true;
    }
    var type = navigator.connection.type;
    var _type = type;
    var _online = type != Connection.NONE;
    if (!_online) {
        systemNotification("网络异常，请连接网络");
    }
    return _online;
};

function buttonClick(item) {
    var _id = localStorage.getItem("id") || "0";
    if (_id != "0" && item != null) {
        $.ajax({
            type: "GET",
            url: page.service + "Click",
            data: {"id": _id, "item": item},
            dataType: "jsonp",
            success: function(e){
                console.log(e);
            },
            error: function(xhr, type){
                $.ui.loading("hide");
            }
        });
    }
}






